package com.compty.paint.service.impl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.compty.core.framework.util.StringUtil;
import com.compty.paint.service.ISmsService;

public class SmsServiceImpl implements ISmsService {

	static String _uc = "1748";// 帐号
	static String _pwd = StringUtil.md5("18925210869"); //密码
	static String _host = "http://kltx.sms10000.com.cn/sdk/SMS?";

	/**
	 * @return 短信编号
	 */
	public String sendMessage(String mobile, String smsCode) {
		// TODO Auto-generated method stub
		String content = "尊敬的可观艺术网用户，您本次申请的短信激活码为："+smsCode+"，激活码30分钟内有效。";
		String msgid = mobile+StringUtil.getRandStr(6);
		String result = sendSMS(mobile, content,msgid);
		if("100".equals(result)){
			return msgid;
		}
		else{
			return null ;
		}
	}

	/**
	 *发送短信
	 * 
	 * @param mobiles
	 *            String 接收号码
	 * @param cont
	 *            String 短信内容
	 * @param msgid
	 *            String 短信ID
	 * @return String
	 */
	public String sendSMS(String mobiles, String cont, String msgid) {
		String re = "";
		try {
			cont = URLEncoder.encode(cont, "GBK"); // 短信内容需要编码
			String sendUrl = _host + "cmd=send&uid=" + _uc + "&psw="
					+ _pwd + "&mobiles=" + mobiles + "&msgid="
					+ msgid + "&msg=" + cont + " ";
			re = submit(sendUrl);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return re;
	}

	/**
	 * 接收短信
	 * 
	 * @return String
	 */
	public String getMO() {
		String re = "";
		try {
			String moUrl = _host + "cmd=getmo&uid=" + _uc + "&psw="
					+ _pwd + "";
			re = submit(moUrl);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return re;
	}

	/**
	 * 取发送状态
	 * 
	 * @return String
	 */
	public String getStatus() {
		String re = "";
		String getstatusUrl = _host + "cmd=getstatus&uid=" + _uc + "&psw="
				+ _pwd + "";
		re = submit(getstatusUrl);
		return re;
	}

	/**
	 * GET提交
	 * 
	 * @param url
	 *            String
	 * @return String
	 */
	private String submit(String url) {
		String re = "";
		int byteMax = 50;
		URL myurl = null;
		URLConnection urlcon = null;
		try {
			myurl = new URL(url);
			urlcon = myurl.openConnection();
			urlcon.setUseCaches(false);
			urlcon.connect();
			InputStream inputstream = urlcon.getInputStream();
			byte[] buff = new byte[byteMax];
			ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
			int ins;
			while ((ins = inputstream.read(buff)) >= 0) {
				bytearrayoutputstream.write(buff, 0, ins);
			}
			re = new String(bytearrayoutputstream.toByteArray());
			re.trim();
			bytearrayoutputstream.close();
			inputstream.close();
		} catch (MalformedURLException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			/** @todo Handle this exception */
			ex.printStackTrace();
		}finally{
			urlcon  = null;
			myurl = null;
		}

		return re;
	}

	
	public static void main(String[] args) {
		SmsServiceImpl serviceImpl = new SmsServiceImpl();
		serviceImpl.sendMessage("13723707256", "787878");
	}

}
